#include "selectview.h"
#include "toolkit.h"
#include <QLocale>
#include "item/imageitem.h"
#include <QMouseEvent>
#include "commomdef.h"
#include <QSound>

selectview::selectview(QWidget *parent) :
    QGraphicsView(parent)
{
    setFrameShape(QFrame::NoFrame);

    m_Scene = new QGraphicsScene(this);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setScene(m_Scene);
    m_Scene->setSceneRect(0.0, 0.0, 640.0, 360.0);
    setGeometry(0.0,0.0,640.0,360.0);

    switch(ToolKit::m_language)
    {
    case QLocale::Chinese:
    {
        m_bgItem = new ImageItem(QImage(":/image/selectui/selectbg1.jpg"),
                                 QRect(QPoint(0,0),QImage(":/image/selectui/selectbg1.jpg").size()));
    }
        break;
    case QLocale::HongKong:
    case QLocale::Taiwan:
    {
        m_bgItem = new ImageItem(QImage(":/image/selectui/selectbg2.jpg"),
                                 QRect(QPoint(0,0),QImage(":/image/selectui/selectbg2.jpg").size()));
    }
        break;
    default:
    {
        m_bgItem = new ImageItem(QImage(":/image/selectui/selectbg.jpg"),
                                 QRect(QPoint(0,0),QImage(":/image/selectui/selectbg.jpg").size()));
    }
        break;
    }

    m_bgItem->setZValue(0.0);
    m_bgItem->setPos(0,0);
    m_Scene->addItem(m_bgItem);

    initBtn();
    ShowBtn();
}

void selectview::mousePressEvent(QMouseEvent *event)
{
    if(QRectF(265.0,335.0,105.0,25.0).contains(event->pos()))
    {
        if(ToolKit::m_bVoice)
            ToolKit::playSound(4);

        emit back();
    }
    else
    {
        foreach(ImageItem* item,m_btnlist)
        {
            if(!item->isVisible() && item->isUnderMouse())
            {
                if(ToolKit::m_bVoice)
                    ToolKit::playSound(29);

                ToolKit::m_curlevel = m_btnlist.indexOf(item);

                emit startgame();
                break;
            }
        }
    }
}

void selectview::initBtn()
{
    int irow(0),iline(0);
    QString str(":/image/selectui/db_1.png");

    for(int i = 0; i < 20; ++i)
    {
        ImageItem* item = new ImageItem(QImage(str),
                                        QRect(QPoint(0,0),QImage(str).size()));
        item->setPos(SELBTN_START_X + SELBTN_X_INTERVAL * irow,
                     SELBTN_START_Y + SELBTN_Y_INTERVAL * iline++);

        if(iline % 5 == 0)
        {
            iline = 0;
            ++irow;
            str = QString(":/image/selectui/db_%1.png").arg(irow + 1);
        }

        m_Scene->addItem(item);
        m_btnlist.append(item);
    }
}

void selectview::ShowBtn()
{
    for(int i = 0; i <= ToolKit::m_level; ++i)
    {
        m_btnlist.at(i)->setVisible(false);
    }

    for(int j = ToolKit::m_level + 1; j < m_btnlist.count(); ++j)
    {
        m_btnlist.at(j)->setVisible(true);
    }
}
